function UCharacter() {
	var _uKey = UKey.getInstance();
	var _uData = UData.getInstance();
	var _message = UMessage.getInstance();
	var _character = null;
	var self = this;
	
	this.show = function(urlObj, options) {
		var id = urlObj.hash.replace( /.*cId=/, "" );
		_character = _uData.findCharacterById(id);
		if ( _character == null ) {
			return;
		}

		var pageSelector = urlObj.hash.replace( /\?.*$/, "" );
		var $page = $( pageSelector );
		
		var $header = $page.children( ":jqmData(role=header)" );
//		var $content = $page.children( ":jqmData(role=content)" );
		
		var subT;
		var content = '';
		switch (_character.type) {
		case UUtil.CHARACTER_TYPE_OrderedMultiState:
		case UUtil.CHARACTER_TYPE_UnorderedMultiState:
			subT = 'Please select state or states';
			var states = _character.states;
			if (!states || states.length < 1) {
				return;
			}
			
			content += '<div style="width:260px;" data-role="fieldcontain">';
			content += '<fieldset data-role="controlgroup">';
			content += '';
			for (var i=0; i<states.length; i++) {
				$msg = _message.getOnly('c' + _character.id + "_" + i);
				if ( UUtil.isNull($msg)) {
					$msg = states[i];
				}
				var s = $msg;
				
				if ( UUtil.isNull(s)) {
					continue;
				}
				
				var checked = '';
				if ( isIn(_character.selectedValues, i+1) ) {
					checked = ' Checked="true"';
				}
				
				var n = 'uPC_select' + i;				
				if ( !_character.isE ) {
					content += '<input ' + checked + ' type="radio" name="uPC_select" id="' + n + '" value="' + (i+1) + '" data-inline="true" data-theme="c"  />';
					content += '<label for="' + n + '">' + s + '</label>';
				} else {
					content += '<input ' + checked + ' type="checkbox" name="uPC_select" id="' + n + '" value="' + (i+1) + '" data-inline="true" data-theme="c" />';
					content += '<label for="' + n + '">' + s + '</label>';
				}					
			}				
			 
			content += '</fieldset>';
			content += '</div>';
			break;
		default:
			subT = 'Please enter the value';
			content += '<br/><input style="width:260px;" value="'
				+ UUtil.nullToNone(_character.enteredValue) + '" type="text" name="uPC_enteredValue" id="uPC_enteredValue" data-inline="true"/>';
			if ( !UUtil.isNull(_character.un) ) {
				content +=_character.un;				
			}
			break;
		}
		$header.find("h1").html( subT );	
		$msg = _message.getOnly('c' + _character.id);
		if ( UUtil.isNull($msg)) {
			$msg = _character.name;
		}
		$('#uPC_cTitle').html(UUtil.escapeHTML($msg));
		$('#uPC_error').hide();
		$('#uPC_content').html(content).trigger("create");
		
		$page.page();
		options.dataUrl = urlObj.href;
		$.mobile.changePage( $page, options );
	};
	
	function isIn(list, val) {
		if ( !list || list.length < 1) {
			return;
		}
		for (var i=0; i<list.length; i++) {
			if ( list[i] == val) {
				return true;
			}
		}
		return false;
	}
	
	this.setValue = function() {
		if ( !_character ) {
			return;
		}
		switch (_character.type) {
		case UUtil.CHARACTER_TYPE_OrderedMultiState:
		case UUtil.CHARACTER_TYPE_UnorderedMultiState:
			var values = [];
			$('input[name="uPC_select"]:checked').each(function() { 
				var v = $(this).val();
				if ( !UUtil.isNull(v)) {
					values.push(v);
				}		
			});
			if ( !values || values.length < 1) {
				$('#uPC_error').html('Please select a value');
				$('#uPC_error').show();
				return;
			}
			_character.selectedValues = values;
			break;
		default:			
			var value = $('#uPC_enteredValue').val();
			if ( UUtil.isNull(value)) {
				$('#uPC_error').html('Please enter the value');
				$('#uPC_error').show();
				return;
			}
			_character.enteredValue = value; 
			break;
		}
		
		_uData.selectCharacter(_character);
	};
	
	this.removeCharacter = function(id) {
		_uData.removeCharacter(id);
	}
	
	function close() {
		$('#uPageTaxon').dialog('close');
	}	
}

UCharacter.getInstance = function() {
	if ( UCharacter._instance == null ) {
		UCharacter._instance = new UCharacter();
	}
	return UCharacter._instance;
};

